package com.yf.tz.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.service.WxPayService;
import com.yf.tz.entity.MallOrderEntity;
import com.yf.tz.enums.MallOrderPaidEnum;
import com.yf.tz.service.MallOrderService;
import com.yf.tz.vo.MallOrderVo;
import com.yf.tz.vo.OrderCommitVo;
import com.yf.tz.vo.res.CommitOrderRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mall/order")
public class ApiMallOrderController {

    @Autowired
    private WxPayService wxPayService;

    /**
     * 提交订单
     */
    @PostMapping("/commit")
    public R commit(@RequestBody OrderCommitVo orderCommitVo, @LoginUserId Long userId, HttpServletRequest request) {
        CommitOrderRes res = orderService.commitOrder(userId, orderCommitVo,request);
        return R.ok(res);
    }


    /**
     * 支付成功回调  TODO 不成功 记录
     * @param body
     * @return
     */
    @RequestMapping("/pay")
    @IgnoreAuth
    public String pay(@RequestBody String body){
      Map<String,String> map =  wxPayService.handleWxNotice(body);
      String orderNo = map.get("out_trade_no");
      MallOrderEntity orderEntity = orderService.findByOrderNo(orderNo);
      //没有支付
      if(!orderEntity.getPaid().equals(MallOrderPaidEnum.YES.getIndex())){
          String totalPay = map.get("total_fee");
         if(orderEntity.getPay().multiply(new BigDecimal(100)).compareTo(new BigDecimal(totalPay))==0){
             orderService.afterPay(orderEntity);
         }
      }
        return "<xml>\n" +
                "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>";
    }

    /**
     * 取消订单
     */
    @PostMapping("/cancel")
    public R cancel(Long orderId, @LoginUserId Long userId) {
        MallOrderEntity orderEntity = orderService.cancelOrder(userId, orderId);
        return R.ok(orderEntity);
    }

    /**
     * 继续支付
     * @param userId
     * @param orderId
     * @return
     */
    @PostMapping("/repay")
    public R repay(@LoginUserId Long userId,Long orderId){
        String res = orderService.rePay(userId,orderId);
        return R.ok(res);
    }

    /**
     * 找出所有订单
     *
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginUserId Long userId, Integer page, Integer limit,Integer paid,Integer status) {
        List<MallOrderVo> list = orderService.findByUserId(userId, paid,status,page, limit);
        return R.ok(list);
    }

    /**
     * 订单详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/info")
    public R info(@LoginUserId Long userId, Long id) {
        MallOrderVo list = orderService.getVo(id);

        return R.ok(list);
    }

    @Autowired
    private MallOrderService orderService;

}
